#ifndef FAVORITESITEMFILTERMODEL_H
#define FAVORITESITEMFILTERMODEL_H
#include <QAbstractItemModel>
#include <QHash>
#include <QPersistentModelIndex>
#include <QPair>

namespace ExecTerminalImplNS
{
    extern const char * const MODEL_MIME_TYPE;//defined in favoritesmodel.cpp
    class FavoritesItemFilterModel: public QAbstractItemModel
    {
        Q_OBJECT
    public:
        FavoritesItemFilterModel(QObject * parent = 0);

        //model access interface
        int columnCount(const QModelIndex & parent = QModelIndex())const;
        int rowCount(const QModelIndex & parent = QModelIndex())const;
        QVariant data(const QModelIndex & index, int role)const;
        QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex())const;
        QModelIndex parent(const QModelIndex & index)const;
        Qt::ItemFlags flags(const QModelIndex &index)const;
        
        Qt::DropActions supportedDropActions()const;
        QStringList mimeTypes()const;
        QMimeData *mimeData(const QModelIndexList &indexes)const;

        //model edit interface:
        bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
        bool removeRows(int row, int count, const QModelIndex & parent);
        void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
        bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
           
        //custom methods
        void setSourceModel(QAbstractItemModel * model);
        QAbstractItemModel * sourceModel()const;
        void setFilterParams(const QString & filterPattern, int role = Qt::DisplayRole);

        //custom methods
        QModelIndex mapToSource(const QModelIndex & filterModelIndex)const;

    public slots:
        void rebuildModelIfNeed();

    protected slots:
        void checkForRebuild();

    protected:
        typedef QList<QPersistentModelIndex> MappingToSource;//holds the source model indexes. list indexes is row numbers in this model

    protected:
        void rebuildModel();
        QModelIndexList findMatchIndexes(const QModelIndex & parentInd);
        void removeOneRow(int rowNum, const QModelIndex & parentInd = QModelIndex());//parentInd should be invalid

    private:
        MappingToSource theMappingToSource;
        QString theFilterPattern;
        mutable QPersistentModelIndex theCurrentDragIndex;
        int theDataRoleToFilter;
        QAbstractItemModel * theSourceModel;
        bool theModelNeedsUpdate;
    };
}

#endif


